package org.qydjk.h5.controller.base;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.qydjk.common.constant.CommonConstant;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.Result;
import org.qydjk.h5.constant.H5Constant;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 控制层基类
 */
public class BaseController {
	protected Logger logger = LogManager.getLogger(getClass());
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected HttpSession session;
	
	@ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response){  
        this.request = request;
		this.request.setAttribute("rootPath",H5Constant.rootPath);
        this.response = response;  
        this.session = request.getSession();
    }

	/**
	 * ajax失败
	 * @param msg 失败的消息
	 * @return {Object}
	 */
	public Object renderError(String msg) {
		Result result = new Result();
		result.setSuccess(false);
		result.setMsg(msg);
		return result;
	}

	/**
	 * ajax成功
	 * @return {Object}
	 */
	public Object renderSuccess() {
		Result result = new Result();
		result.setSuccess(true);
		result.setMore(true);
		return result;
	}

	/**
	 * ajax成功
	 * @param msg 消息
	 * @return {Object}
	 */
	public Object renderSuccess(String msg) {
		Result result = new Result();
		result.setSuccess(true);
		result.setMsg(msg);
		result.setMore(true);
		return result;
	}

	/**
	 * ajax成功
	 * @param obj 成功时的对象
	 * @return {Object}
	 */
	public Object renderSuccess(Object obj) {
		Result result = new Result();
		result.setSuccess(true);
		result.setObj(obj);
		result.setMore(true);
		return result;
	}

	/**
	 * ajax成功
	 * @param list 成功的列表
	 * @param rows 每次请求的最大数量
	 * @return {Object}
	 */
	public Object renderListSuccess(List list, Integer rows) {
		Result result = new Result();
		result.setSuccess(true);
		result.setObj(list);
		if (list.size() <= 0) result.setMore(false);
		else result.setMore(true);
		return result;
	}

	/**
	 * 获取请求字符串
	 * @return
	 */
	public String getRequestString() {
		String reqString = "";
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ServletInputStream sis = null;
		try {
			request.setCharacterEncoding("utf-8");
			sis = request.getInputStream();
			byte[] b = new byte[1024];
			int len = 0;
			while ((len = sis.read(b, 0, b.length)) != -1) {
				baos.write(b, 0, len);
			}
			byte[] buffer = baos.toByteArray();
			reqString = new String(buffer, "utf-8").trim();
			logger.info("req-"+reqString);
		} catch (IOException e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}finally{
			try {
				if(baos != null){
					baos.flush();
					baos.close();
				}
				if(sis != null){
					sis.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return reqString;
	}
	
	/**
	 * json输出
	 * @param res
	 */
	public void output(String res) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out = null;
		try {
			logger.info("res-"+res);
			out = response.getWriter();
			out.print(res);
		} catch (IOException e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}catch (Exception e) {
			e.printStackTrace();
			logger.info(e.getMessage());
		}finally{
			if(out != null){
				out.flush();
				out.close();
			}
		}
	}

	public Integer getUserId() {
		return 3;
	}

	/**
	 * 获取城市ID
	 * @return
	 */
	public Integer getAreaId(){
		Integer areaId = (Integer) session.getAttribute(CommonConstant.COMMON_AREA_ID_SESSION_NAME);
		if(areaId != null && areaId.intValue() > 0){
			return areaId;
		}
		return 0;
	}

	/**
	 * 获取位置经纬度
	 * 数组第一位为lat，第二位为lon
	 * @return
	 */
	public String[] getLatLon(){
		String latLon = (String) session.getAttribute(CommonConstant.COMMON_LON_LAT_SESSION_NAME);
		if(!StringUtil.isEmptyOrNull(latLon)){
			String[] latLonArr = latLon.split("-");
			if(latLonArr.length == 2){
				return latLonArr;
			}
		}
		return null;
	}
}